package com.example.sjly.ui.login_register;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.ColorSpace;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.example.sjly.R;
import com.example.sjly.sqliteroom.AppRoomDatabase;
import com.example.sjly.sqliteroom.User;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class register extends AppCompatActivity {
    private String username;      private EditText editText1;
    private String password="";   ToastUtils toast=null;
    private String password1;     private EditText editText2;
    private String password2;     private EditText editText3;
    private String mail;          private EditText editText4;
    private String school;        private EditText editText5;
    private String phone;         private EditText editText6;
    private Button register;
    User user1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        editText1 =((EditText) findViewById(R.id.editText1));
        editText2 =((EditText) findViewById(R.id.editText2));
        editText3 =((EditText) findViewById(R.id.editText3));
        editText4 =((EditText) findViewById(R.id.editText4));
        editText5 =((EditText) findViewById(R.id.editText5));
        editText6 =((EditText) findViewById(R.id.editText6));

        @SuppressLint("HandlerLeak")
        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
            }
        };

        register =(Button) findViewById(R.id.submit_button);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取文本框的信息
                username =editText1.getText().toString();
                mail     =editText2.getText().toString();
                password1=editText3.getText().toString();
                password2=editText4.getText().toString();
                school   =editText5.getText().toString();
                phone    =editText6.getText().toString();

                if (username != null && !username.equals("")
                        && username.length() > 0) {
                    if (!isEmail(mail)) {
                        Toast.makeText(getApplicationContext(), "邮箱格式不正确", Toast.LENGTH_LONG).show();
                    }
                    else if (!isNumeric(phone) && !isMobileNO(phone)) {
                        Toast.makeText(getApplicationContext(), "手机格式不正确", Toast.LENGTH_LONG).show();
                    }
                    else if (password1.equals("")||password2.equals("")){
                        Toast.makeText(getApplicationContext(),"密码不能为空",Toast.LENGTH_LONG).show();
                    }
                    else if (!password1.equals(password2))
                    {
                        Toast.makeText(getApplicationContext(),"密码不同",Toast.LENGTH_LONG).show();
                    }
                    else{
                        password=password2;
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                addUser(username,password,mail,phone,school);
                            }
                        }).start();
                        finish();
                    }
                }
               else {
                  Toast.makeText(getApplicationContext(),"账号错误",Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    //注册用户
     private void addUser(String username,String password,String mail,String phone,String school) {

        AppRoomDatabase db =AppRoomDatabase.getInstance(getApplicationContext());

        user1=db.userdao().getByName(username);
        if (user1!=null){
            toast.show(this,"账号已存在");

        }
        else {
             User user=new User(username,password,mail,phone,school);
             db.userdao().insert(user);
             toast.show(this,"注册成功");
//            Intent intent = new Intent(this,login.class);
//            startActivity(intent);
         }
    }


    //判断手机格式是否正确
    public boolean isMobileNO(String mobiles) {
        Pattern p = Pattern
                .compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);

        return m.matches();
    }
    //判断email格式是否正确
    public boolean isEmail(String email) {
        String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);

        return m.matches();
    }
    //判断是否全是数字
    public boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    public static class ToastUtils {
        static Toast toast = null;
        public static void show(Context context, String text) {
            try {
                if(toast!=null){
                    toast.setText(text);
                }else{
                    toast= Toast.makeText(context, text, Toast.LENGTH_SHORT);
                }
                toast.show();
            } catch (Exception e) {
                //解决在子线程中调用Toast的异常情况处理
                Looper.prepare();
                Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
                Looper.loop();
            }
        }
    }

}


